import 'package:flutter/material.dart';
import 'pages/login/login_page.dart'; // 登录页面
import 'pages/home/home_page.dart' as home; // 首页


void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '好睡',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
        useMaterial3: true,
      ),
      initialRoute: '/',
      onGenerateRoute: (settings) {
        print('Navigating to ${settings.name} with arguments: ${settings.arguments}');

        if (settings.name == '/') {
          return MaterialPageRoute(builder: (context) => LoginPage(onLoginSuccess: (args) {
            Navigator.pushNamed(context, '/home', arguments: args);
          }));
        } else if (settings.name == '/home') {
          if (settings.arguments is Map<String, dynamic>) {
            final args = settings.arguments as Map<String, dynamic>;
            print('Arguments received: $args');
            return MaterialPageRoute(
              builder: (context) => home.HomePage(email: args['email'], password: args['password']),
            );
          } else {
            print('No arguments provided for /home route');
            return MaterialPageRoute(builder: (context) => LoginPage(onLoginSuccess: (args) {}));
          }
        } else {
          print('Unknown route: ${settings.name}');
          return MaterialPageRoute(builder: (context) => LoginPage(onLoginSuccess: (args) {}));
        }
      },
      // home: const LoginPage(),
    );
  }
}

class NewRoute extends StatelessWidget {
  const NewRoute({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('New Route'),
      ),
      body: const Center(
        child: Text('This is the new route!'),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have clicked the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            // ElevatedButton(
            //   onPressed: () {
            //     // 在这里添加按钮点击事件的处理逻辑
            //     setState(() {
            //       _counter = 0; // 例如，重置计数器
            //     });
            //   },
            //   child: const Text('Reset Counter'),
            // ),
            // 页面跳转
            ElevatedButton(
              onPressed: () {
                // 在这里添加按钮点击事件的处理逻辑
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => const NewRoute()),
                );
              },
              child: const Text('Reset Counter'),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
